package com.magic233.config;

import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.io.File;

/**
 * ClassName: MyWebConfig
 * Description:
 * date: 2021/6/1 15:22
 * author: zouyuan
 */

@Configuration
public class MyWebConfig extends WebMvcConfigurerAdapter {
    //将jar文件下的对应静态资源文件路径对应到磁盘的路径(根据个人的情况修改"file:static/"的static的值
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        ApplicationHome h = new ApplicationHome(getClass());
        File jarF = h.getSource();
        String dirPath = jarF.getParentFile().toString() + "/upload/";

        String os = System.getProperty("os.name");

        System.out.println(os);

        if (os.toLowerCase().startsWith("win")) {  //如果是Windows系统
            registry.addResourceHandler("/upload/**").addResourceLocations("file:" + dirPath);
            System.out.println("file:" + dirPath);

        } else {
            //  docker部署，jar文件在根目录
            dirPath = jarF.getParentFile().toString() + "upload/";
            registry.addResourceHandler("upload/**").addResourceLocations("file:" + dirPath);
            System.out.println("file:" + dirPath);
        }
    }

}

